implement EncoderAlias;

include "sys.m";
include "hash.m";

include "converter.m";

lookup( targ : string ) : string
{
	# lookup the proper name of an alias

	table   := mastertable;

	while( 1 )
	{
		end := len table;
		if ( end <= 20 )
		{
			# linear search
			for( x:=0; x < end; x++ )
			{
				(alias,proper) := table[x];
				if ( alias == targ )
					return( proper );
			}
			# search failed
			return(nil);
		}

		# do binary search
		mid := int end / 2;
		(alias,proper) := table[mid];
		if ( alias == targ )
			return( proper );

		# search failed setup next portion
		if ( alias < targ )
			table = table[mid+1:];
		else
			table = table[:mid-1];
	}		

	return( nil );
}



#
# alias table: each entry is an (alias,proper)
# pair of strings.  This table MUST be in
# sorted order, else the search fails.
# by placing in a initialized module-global
# the resulting size and load/init time is greatly
# reduced.
#
mastertable := array[] of 
{
	("037",                   "Cp037"),
	("1006",                  "Cp1006"),
	("1025",                  "Cp1025"),
	("1026",                  "Cp1026"),
	("1089",                  "8859_6"),
	("1097",                  "Cp1097"),
	("1098",                  "Cp1098"),
	("1112",                  "Cp1112"),
	("1122",                  "Cp1122"),
	("1123",                  "Cp1123"),
	("1124",                  "Cp1124"),
	("1381",                  "Cp1381"),        # MDA 
	("1383",                  "Cp1383"),        # MDA 
	("273",                   "Cp273"),
	("277",                   "Cp277"),
	("278",                   "Cp278"),
	("280",                   "Cp280"),
	("284",                   "Cp284"),
	("285",                   "Cp285"),
	("297",                   "Cp297"),
	("33722",                 "Cp33722"),       # MDA 
	("420",                   "Cp420"),
	("424",                   "Cp424"),
	("437",                   "Cp437"),
	("500",                   "Cp500"),
	("737",                   "Cp737"),
	("775",                   "Cp775"),
	("813",                   "8859_7"),
	("819",                   "8859_1"),
	("838",                   "Cp838"),         # MDA 
	("850",                   "Cp850"),
	("852",                   "Cp852"),
	("855",                   "Cp855"),
	("856",                   "Cp856"),
	("857",                   "Cp857"),
	("860",                   "Cp860"),
	("861",                   "Cp861"),
	("862",                   "Cp862"),
	("863",                   "Cp863"),
	("865",                   "Cp865"),
	("866",                   "Cp866"),
	("868",                   "Cp868"),
	("869",                   "Cp869"),
	("870",                   "Cp870"),
	("871",                   "Cp871"),
	("874",                   "Cp874"),
	("875",                   "Cp875"),
	("912",                   "8859_2"),
	("913",                   "8859_3"),
	("914",                   "8859_4"),
	("915",                   "8859_5"),
	("916",                   "8859_8"),
	("918",                   "Cp918"),
	("920",                   "8859_9"),
	("921",                   "Cp921"),
	("922",                   "Cp922"),
	("930",                   "Cp930"),         # MDA 
	("933",                   "Cp933"),         # MDA 
	("935",                   "Cp935"),         # MDA 
	("937",                   "Cp937"),         # MDA 
	("939",                   "Cp939"),         # MDA 
	("942",                   "Cp942"),         # MDA 
	("948",                   "Cp948"),         # MDA 
	("949",                   "Cp949"),         # MDA 
	("950",                   "Cp950"),         # MDA 
	("964",                   "Cp964"),         # MDA 
	("970",                   "Cp970"),         # MDA 
	("arabic",                "8859_6"),
	("asmo-708",              "8859_6"),
	("cp-gr",                 "Cp869"),
	("cp-is",                 "Cp861"),
	("cp037",                 "Cp037"),
	("cp1006",                "Cp1006"),
	("cp1025",                "Cp1025"),
	("cp1026",                "Cp1026"),
	("cp1089",                "8859_6"),
	("cp1097",                "Cp1097"),
	("cp1098",                "Cp1098"),
	("cp1112",                "Cp1112"),
	("cp1122",                "Cp1122"),
	("cp1123",                "Cp1123"),
	("cp1124",                "Cp1124"),
	("cp1381",                "Cp1381"),        # MDA 
	("cp1383",                "Cp1383"),        # MDA 
	("cp273",                 "Cp273"),
	("cp277",                 "Cp277"),
	("cp278",                 "Cp278"),
	("cp280",                 "Cp280"),
	("cp284",                 "Cp284"),
	("cp285",                 "Cp285"),
	("cp297",                 "Cp297"),
	("cp33722",               "Cp33722"),       # MDA 
	("cp420",                 "Cp420"),
	("cp424",                 "Cp424"),
	("cp437",                 "Cp437"),
	("cp500",                 "Cp500"),
	("cp737",                 "Cp737"),
	("cp775",                 "Cp775"),
	("cp813",                 "8859_7"),
	("cp819",                 "8859_1"),
	("cp838",                 "Cp838"),         # MDA 
	("cp850",                 "Cp850"),
	("cp852",                 "Cp852"),
	("cp855",                 "Cp855"),
	("cp856",                 "Cp856"),
	("cp857",                 "Cp857"),
	("cp860",                 "Cp860"),
	("cp861",                 "Cp861"),
	("cp862",                 "Cp862"),
	("cp863",                 "Cp863"),
	("cp864",                 "Cp864"),
	("cp865",                 "Cp865"),
	("cp866",                 "Cp866"),
	("cp868",                 "Cp868"),
	("cp869",                 "Cp869"),
	("cp870",                 "Cp870"),
	("cp871",                 "Cp871"),
	("cp874",                 "Cp874"),
	("cp875",                 "Cp875"),
	("cp912",                 "8859_2"),
	("cp913",                 "8859_3"),
	("cp914",                 "8859_4"),
	("cp915",                 "8859_5"),
	("cp916",                 "8859_8"),
	("cp918",                 "Cp918"),
	("cp920",                 "8859_9"),
	("cp921",                 "Cp921"),
	("cp922",                 "Cp922"),
	("cp930",                 "Cp930"),         # MDA 
	("cp933",                 "Cp933"),         # MDA 
	("cp935",                 "Cp935"),         # MDA 
	("cp937",                 "Cp937"),         # MDA 
	("cp939",                 "Cp939"),         # MDA 
	("cp942",                 "Cp942"),         # MDA 
	("cp948",                 "Cp948"),         # MDA 
	("cp949",                 "Cp949"),         # MDA 
	("cp950",                 "Cp950"),         # MDA 
	("cp964",                 "Cp964"),         # MDA 
	("cp970",                 "Cp970"),         # MDA 
	("csISOLatin1",           "8859_1"),
	("cseucpkdfmtjapanese",   "EUCJIS"),
	("csibm857",              "Cp857"),
	("csibm860",              "Cp860"),
	("csibm861",              "Cp861"),
	("csibm863",              "Cp863"),
	("csibm864",              "Cp864"),
	("csibm865",              "Cp865"),
	("csibm865",              "Cp866"),
	("csibm869",              "Cp869"),
	("csiso2022jp",           "JIS"),
	("csisolatin2",           "8859_2"),
	("csisolatin3",           "8859_3"),
	("csisolatin4",           "8859_4"),
	("csisolatin5",           "8859_9"),
	("csisolatinarabic",      "8859_6"),
	("csisolatincyrillic",    "8859_5"),
	("csisolatingreek",       "8859_7"),
	("csisolatinhebrew",      "8859_8"),
	("csjisencoding",         "JIS"),
	("cskoi8r",               "KOI8_R"),
	("cspc850multilingual",   "Cp850"),
	("cspc862latinhebrew",    "Cp862"),
	("cspc8codepage437",      "Cp437"),
	("cspcp852",              "Cp852"),
	("cspcp855",              "Cp855"),
	("csshiftjis",            "SJIS"),
	("cswindows31j",          "SJIS"),
	("cyrillic",              "8859_5"),
	("ecma-114",              "8859_6"),
	("ecma-118",              "8859_7"),
	("elot_928",              "8859_7"),
	("extended_unix_code_packed_format_for_japanese", "EUCJIS"),
	("greek",                 "8859_7"),
	("greek8",                "8859_7"),
	("hebrew",                "8859_8"),
	("ibm-037",               "Cp037"),
	("ibm-1006",              "Cp1006"),
	("ibm-1025",              "Cp1025"),
	("ibm-1026",              "Cp1026"),
	("ibm-1089",              "8859_6"),
	("ibm-1097",              "Cp1097"),
	("ibm-1098",              "Cp1098"),
	("ibm-1112",              "Cp1112"),
	("ibm-1122",              "Cp1122"),
	("ibm-1123",              "Cp1123"),
	("ibm-1124",              "Cp1124"),
	("ibm-1381",              "Cp1381"),        # MDA 
	("ibm-1383",              "Cp1383"),        # MDA 
	("ibm-273",               "Cp273"),
	("ibm-277",               "Cp277"),
	("ibm-278",               "Cp278"),
	("ibm-280",               "Cp280"),
	("ibm-284",               "Cp284"),
	("ibm-285",               "Cp285"),
	("ibm-297",               "Cp297"),
	("ibm-33722",             "Cp33722"),       # MDA 
	("ibm-420",               "Cp420"),
	("ibm-424",               "Cp424"),
	("ibm-437",               "Cp437"),
	("ibm-500",               "Cp500"),
	("ibm-737",               "Cp737"),
	("ibm-775",               "Cp775"),
	("ibm-813",               "8859_7"),
	("ibm-819",               "8859_1"),
	("ibm-838",               "Cp838"),         # MDA 
	("ibm-850",               "Cp850"),
	("ibm-852",               "Cp852"),
	("ibm-855",               "Cp855"),
	("ibm-856",               "Cp856"),
	("ibm-857",               "Cp857"),
	("ibm-860",               "Cp860"),
	("ibm-861",               "Cp861"),
	("ibm-862",               "Cp862"),
	("ibm-863",               "Cp863"),
	("ibm-864",               "Cp864"),
	("ibm-865",               "Cp865"),
	("ibm-866",               "Cp866"),
	("ibm-868",               "Cp868"),
	("ibm-869",               "Cp869"),
	("ibm-870",               "Cp870"),
	("ibm-871",               "Cp871"),
	("ibm-874",               "Cp874"),
	("ibm-875",               "Cp875"),
	("ibm-912",               "8859_2"),
	("ibm-913",               "8859_3"),
	("ibm-914",               "8859_4"),
	("ibm-915",               "8859_5"),
	("ibm-916",               "8859_8"),
	("ibm-918",               "Cp918"),
	("ibm-920",               "8859_9"),
	("ibm-921",               "Cp921"),
	("ibm-922",               "Cp922"),
	("ibm-930",               "Cp930"),         # MDA 
	("ibm-933",               "Cp933"),         # MDA 
	("ibm-935",               "Cp935"),         # MDA 
	("ibm-937",               "Cp937"),         # MDA 
	("ibm-939",               "Cp939"),         # MDA 
	("ibm-942",               "Cp942"),         # MDA 
	("ibm-948",               "Cp948"),         # MDA 
	("ibm-949",               "Cp949"),         # MDA 
	("ibm-950",               "Cp950"),         # MDA 
	("ibm-964",               "Cp964"),         # MDA 
	("ibm-970",               "Cp970"),         # MDA 
	("ibm037",                "Cp037"),
	("ibm1006",               "Cp1006"),
	("ibm1025",               "Cp1025"),
	("ibm1026",               "Cp1026"),
	("ibm1089",               "8859_6"),
	("ibm1097",               "Cp1097"),
	("ibm1098",               "Cp1098"),
	("ibm1112",               "Cp1112"),
	("ibm1122",               "Cp1122"),
	("ibm1123",               "Cp1123"),
	("ibm1124",               "Cp1124"),
	("ibm1381",               "Cp1381"),        # MDA 
	("ibm1383",               "Cp1383"),        # MDA 
	("ibm273",                "Cp273"),
	("ibm277",                "Cp277"),
	("ibm278",                "Cp278"),
	("ibm280",                "Cp280"),
	("ibm284",                "Cp284"),
	("ibm285",                "Cp285"),
	("ibm297",                "Cp297"),
	("ibm33722",              "Cp33722"),       # MDA 
	("ibm420",                "Cp420"),
	("ibm424",                "Cp424"),
	("ibm437",                "Cp437"),
	("ibm500",                "Cp500"),
	("ibm737",                "Cp737"),
	("ibm775",                "Cp775"),
	("ibm813",                "8859_7"),
	("ibm819",                "8859_1"),
	("ibm838",                "Cp838"),         # MDA 
	("ibm850",                "Cp850"),
	("ibm852",                "Cp852"),
	("ibm855",                "Cp855"),
	("ibm856",                "Cp856"),
	("ibm857",                "Cp857"),
	("ibm860",                "Cp860"),
	("ibm861",                "Cp861"),
	("ibm862",                "Cp862"),
	("ibm863",                "Cp863"),
	("ibm864",                "Cp864"),
	("ibm865",                "Cp865"),
	("ibm866",                "Cp866"),
	("ibm868",                "Cp868"),
	("ibm869",                "Cp869"),
	("ibm870",                "Cp870"),
	("ibm871",                "Cp871"),
	("ibm874",                "Cp874"),
	("ibm875",                "Cp875"),
	("ibm912",                "8859_2"),
	("ibm913",                "8859_3"),
	("ibm914",                "8859_4"),
	("ibm915",                "8859_5"),
	("ibm916",                "8859_8"),
	("ibm918",                "Cp918"),
	("ibm920",                "8859_9"),
	("ibm921",                "Cp921"),
	("ibm922",                "Cp922"),
	("ibm930",                "Cp930"),         # MDA 
	("ibm933",                "Cp933"),         # MDA 
	("ibm935",                "Cp935"),         # MDA 
	("ibm937",                "Cp937"),         # MDA 
	("ibm939",                "Cp939"),         # MDA 
	("ibm942",                "Cp942"),         # MDA 
	("ibm948",                "Cp948"),         # MDA 
	("ibm949",                "Cp949"),         # MDA 
	("ibm950",                "Cp950"),         # MDA 
	("ibm964",                "Cp964"),         # MDA 
	("ibm970",                "Cp970"),         # MDA 
	("iso-2022-jp",           "JIS"),
	("iso-8859-1",            "8859_1"),
	("iso-8859-2",            "8859_2"),
	("iso-8859-3",            "8859_3"),
	("iso-8859-4",            "8859_4"),
	("iso-8859-5",            "8859_5"),
	("iso-8859-6",            "8859_6"),
	("iso-8859-7",            "8859_7"),
	("iso-8859-8",            "8859_8"),
	("iso-8859-9",            "8859_9"),
	("iso-ir-100",            "8859_1"),
	("iso-ir-101",            "8859_2"),
	("iso-ir-109",            "8859_3"),
	("iso-ir-110",            "8859_4"),
	("iso-ir-126",            "8859_7"),
	("iso-ir-127",            "8859_6"),
	("iso-ir-138",            "8859_8"),
	("iso-ir-144",            "8859_5"),
	("iso-ir-148",            "8859_9"),
	("iso_8859-1",            "8859_1"),
	("iso_8859-1:1978",       "8859_1"),
	("iso_8859-2",            "8859_2"),
	("iso_8859-2:1987",       "8859_2"),
	("iso_8859-3",            "8859_3"),
	("iso_8859-3:1988",       "8859_3"),
	("iso_8859-4",            "8859_4"),
	("iso_8859-4:1988",       "8859_4"),
	("iso_8859-5",            "8859_5"),
	("iso_8859-5:1988",       "8859_5"),
	("iso_8859-6",            "8859_6"),
	("iso_8859-6:1987",       "8859_6"),
	("iso_8859-7",            "8859_7"),
	("iso_8859-7:1987",       "8859_7"),
	("iso_8859-8",            "8859_8"),
	("iso_8859-8:1988",       "8859_8"),
	("iso_8859-9",            "8859_9"),
	("iso_8859-9:1989",       "8859_9"),
	("jis auto detect",       "JISAutoDetect"),
	("jis_encoding",          "JIS"),
	("koi8-r",                "KOI8_R"),
	("ks_c_5601-1987",        "KSC5601"),
	("ksc_5601",              "KSC5601"),
	("l1",                    "8859_1"),
	("l2",                    "8859_2"),
	("l3",                    "8859_3"),
	("l4",                    "8859_4"),
	("l5",                    "8859_9"),
	("latin1",                "8859_1"),
	("latin2",                "8859_2"),
	("latin3",                "8859_3"),
	("latin4",                "8859_4"),
	("latin5",                "8859_9"),
	("ms_kanji",              "SJIS"),
	("shift_jis",             "SJIS"),
	("windows-1250",          "Cp1250"),
	("windows-1251",          "Cp1251"),
	("windows-1252",          "Cp1252"),
	("windows-1253",          "Cp1253"),
	("windows-1254",          "Cp1254"),
	("windows-1255",          "Cp1255"),
	("windows-1256",          "Cp1256"),
	("windows-1257",          "Cp1257"),
	("windows-1258",          "Cp1258"),
	("windows-31J",           "SJIS"),
	("windows-874",          	"MS874")
};

